package responses

import (
	"github.com/edgexfoundry/edgex-go/internal/app/dtos"
	"github.com/edgexfoundry/edgex-go/internal/app/models"
	edgexDtos "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos"
	"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/common"
)

type DeviceAutoEventResponse struct {
	common.BaseResponse `json:",inline"`
	AutoEvents          []dtos.AutoEvent `json:"data"`
}

// NewDeviceAutoEventResponse creates deviceResource response DTO with required fields
func NewDeviceAutoEventResponse(requestId string, message string, statusCode int, events []models.AutoEvent) DeviceAutoEventResponse {
	return DeviceAutoEventResponse{
		BaseResponse: common.NewBaseResponse(requestId, message, statusCode),
		AutoEvents:   ToAutoEventDTOs(events),
	}
}

func ToAutoEventDTOs(aes []models.AutoEvent) []dtos.AutoEvent {
	events := make([]dtos.AutoEvent, len(aes))
	for index, ae := range aes {
		events[index] = ToAutoEventDTO(ae)
	}
	return events
}

func ToAutoEventDTO(d models.AutoEvent) dtos.AutoEvent {
	return dtos.AutoEvent{
		AutoEvent:  edgexDtos.FromAutoEventModelToDTO(d.AutoEvent),
		Id:         d.Id,
		SourceCode: d.SourceCode,
		SourceType: d.SourceType,
	}
}
